/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.action;

import au.edu.unsw.bookmarket.actionform.BookBean;
import au.edu.unsw.bookmarket.actionform.CourseBean;
import au.edu.unsw.bookmarket.actionform.InsertDataBean;
import au.edu.unsw.bookmarket.actionform.SellerBean;
import au.edu.unsw.bookmarket.jdo.*;
import au.edu.unsw.bookmarket.service.BookService;
import au.edu.unsw.bookmarket.service.CourseService;
import au.edu.unsw.bookmarket.service.SellerService;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author gongz
 */
public class InsertDataAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    BookService bookService;
    CourseService courseService;
    SellerService sellerService;

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        InsertDataBean insertDataBean = (InsertDataBean) form;
        if (insertDataBean.isInsertPostgradBooks()) {
            readFile("postgrad_books.txt");
            System.out.println("postgrad_books inserted!");
        }
        if (insertDataBean.isInsertPostgradCourses()) {
            readFile("postgrad_courses.txt");
            System.out.println("postgrad_courses inserted!");
        }
        if (insertDataBean.isInsertUndergradBooks()) {
            readFile("undergrad_books.txt");
            System.out.println("undergrad_books inserted!");
        }
        if (insertDataBean.isInsertUndergradCourses()) {
            readFile("undergrad_courses.txt");
            System.out.println("undergrad_courses inserted!");
        }
        return mapping.findForward(SUCCESS);
    }

    public void readFile(String filename) {
        File file = new File(filename);
        BufferedReader reader = null;
        try {
            System.out.println(file.getAbsolutePath());
            reader = new BufferedReader(new FileReader(file));
            if (filename.equals("postgrad_courses.txt")) {
                processingCourse(reader);
            } else if (filename.equals("postgrad_books.txt")) {
                processingTextbook(reader);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void processingCourse(BufferedReader reader) {
        try {
            String text = null;
            int counter = 0;
            while ((text = reader.readLine()) != null) {
                if (text.trim().equals("")) {
                    continue;
                }
                String[] tuple = text.split("\\|", -2);
                if (!tuple[0].trim().equals("")) {
                    Course course = new Course(tuple[0], tuple[3], tuple[2], tuple[1], "", "", true);
                    courseService.addCourseDirectly(course);
                    counter++;
                }
            }
            System.out.println(counter + " Courses added");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void processingTextbook(BufferedReader reader) {
        try {
            String text = null;
            int counter = 0;
            while ((text = reader.readLine()) != null) {
                if (text.trim().equals("")) {
                    continue;
                }
                System.out.println(text);
                String[] tuple = text.split("\\|", -2);
                if (!tuple[1].trim().equals("")) {
                    Book book = new Book(tuple[1], tuple[6], tuple[8], 0, tuple[4], tuple[7], tuple[3], Double.parseDouble(tuple[5]), true);
                    bookService.addBookDirectly(book);
                    counter++;
                }
            }
            System.out.println(counter + " Books added");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
